﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace newTurtle8
{
    public partial class ZimunimCard : UserControl
    {
        Mofa mofa;
        DataTable dt = new DataTable();
        SqlDataAdapter da;
        public SqlDataAdapter SqlDataAdapter
        {
            get { return da; }
            set { da = value; }
        }

        public ZimunimCard(Mofa mofa)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            this.mofa = mofa;
            mofa.ZimunimList_Modified += new Mofa.ZimunimListChangedHandler(refresh);

            SqlCommand selectCmd = SqlManager.getSqlCommandFromFile(@"\SELECT\ZimunimCard.SELECT.sql");
            da = SqlManager.SqlDataAdapterFactory(selectCmd.CommandText.Replace("Table", mofa.ZimunimTableName));
            da.UpdateCommand = SqlManager.getSqlCommandFromFile(@"\UPDATE\ZimunimCard.UPDATE.sql");

            //retreiving a list with all the parameters in the querys
            ICollection<string> sqlParameterCollection = SqlManager.parseSqlQueryParameters(da.UpdateCommand.CommandText);
            sqlParameterCollection.Distinct<string>();

            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
            {
                string sItem = "@" + item;
                switch (item)
                {
                    case "armyID": 
                        da.UpdateCommand.Parameters.Add(SqlManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Original, "armyID"));
                        break;
                    case "comments":
                        da.UpdateCommand.Parameters.Add(SqlManager.createSqlParameter("@comments", SqlDbType.NVarChar, DataRowVersion.Current, "comments"));
                        break;

                    case "startDate":
                        DateTime dt1 = mofa.StartDate;
                        da.UpdateCommand.Parameters.Add(new SqlParameter("@startDate", SqlDbType.Date)).Value = dt1;
                        da.SelectCommand.Parameters.Add(new SqlParameter("@startDate", SqlDbType.Date)).Value = dt1;
                        break;

                    default:
                        da.UpdateCommand.Parameters.Add(SqlManager.createSqlParameter("@" + item, SqlDbType.Bit, DataRowVersion.Current, item));
                        break;
                }
            }

            refresh();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridView.SelectedRows;
            List<string> armyIdList = new List<string>();

            //creates a list of armyID's
            foreach (DataGridViewRow row in selectedRows)
            {
                armyIdList.Add((string)row.Cells["armyID"].Value);
                dataGridView.Rows.Remove(row);
            }
               
            //announce the mofa object that there are some soldier removals     
            mofa.removeSoldier(armyIdList);            
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            SoldiersView z_list = new SoldiersView(mofa, SoldiersView.Type.NotCalled, new DelegateHandler(mofa.addSoldier));
            z_list.Text = "רשימת לוחמים פעילים אשר טרם זומנו";
            z_list.ShowDialog();
        }

        public void refresh() 
        {
            dt.Clear();
            da.Fill(dt);

            dataGridView.DataSource = dt;
            Translator.setTableColumnHeadersHebrew(dataGridView.Columns);
            dataGridView.Refresh();

            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView.Columns["comments"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView.Columns["firstName"].ReadOnly = true;
            dataGridView.Columns["lastName"].ReadOnly = true;
            dataGridView.Columns["armyID"].ReadOnly = true;

            dataGridView.Columns["startDate"].Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            labelSuccessfulUpdate.Visible = false;
        }

    }
}
